<!DOCTYPE html>
<html lang="zh">
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">
            <el-button type="primary" class="butT" @click="addMenu()">添加一级菜单</el-button>
            <el-tree :data="menuData" node-key="id">
                <span class="custom-tree-node" slot-scope="{data}">
                    <span>{{data.name}}</span>
                    <el-button type="text" size="mini" @click="addChildren(data)">
                        <i class="el-icon-plus" aria-hidden="true"></i>
                    </el-button>
                    <el-button type="text" size="mini" @click="editMenu(data)">
                        <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                    </el-button>
                    <el-button type="text" size="mini" @click="deleteMenu(data)">
                        <i class="fa fa-times" aria-hidden="true"></i>
                    </el-button>
                </span>
            </el-tree>
        </div>

        <!-- 新增菜单UI -->
        <div id="addMenuDiv">
            <el-dialog title="新增菜单" :visible.sync="dialogAddMenuVisible">
                <el-form :model="menu">
                    <el-form-item label="菜单名称" :label-width="formLabelWidth">
                        <el-input v-model="menu.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="linkUrl" :label-width="formLabelWidth">
                        <el-input v-model="menu.linkUrl" autocomplete="off"></el-input>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogAddMenuVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handlerAddMenu()">确 定</el-button>
                </div>
            </el-dialog>
        </div>

        <!-- 编辑菜单UI -->
        <div id="editMenuDiv">
            <el-dialog title="编辑菜单" :visible.sync="dialogEditMenuVisible">
                <el-form :model="menu">
                    <el-form-item label="菜单名称" :label-width="formLabelWidth">
                        <el-input v-model="menu.name" autocomplete="off" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="linkUrl" :label-width="formLabelWidth">
                        <el-input v-model="menu.linkUrl" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Font Awesome 图标" :label-width="formLabelWidth">
                        <el-input v-model="menu.icon" autocomplete="off"></el-input>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogEditMenuVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handlerEditMenu()">确 定</el-button>
                </div>
            </el-dialog>
        </div>

    </div>
</div>

<!-- 引入组件库 -->
<script src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            menuData: [],
            dialogAddMenuVisible: false,
            dialogEditMenuVisible: false,
            formLabelWidth: '120px',
            menu: {
                name: '',
                linkUrl: '',
                parentMenuId: null
            }
        },

        // 页面加载后读取菜单
        created() {
            this.loadMenu();
        },

        methods: {
            // 添加子菜单
            addChildren(data) {
                this.menu = {};
                this.dialogAddMenuVisible = true;
                this.menu.parentMenuId = data.id;
                this.menu.level = data.level + 1;
                this.menu.del = 0;
                let size = data.children.length;
                if (size > 0) {
                    this.menu.priority = data.children[size - 1].priority + 1;
                } else {
                    this.menu.priority = 1;
                }
                if (this.menu.level === 2) {
                    this.menu.path = "/" + data.path + "-" + this.menu.priority;
                }
                if (this.menu.level > 2) {
                    this.menu.path = data.path + "-" + this.menu.priority;
                }
            },
            // 添加一级菜单
            addMenu() {
                this.menu = {};
                this.dialogAddMenuVisible = true;
                this.menu.level = 1;
                this.menu.del = 0;
                this.menu.icon = "fa-rocket";
                let size = this.menuData.length;
                if (size > 0) {
                    this.menu.priority = this.menuData[size - 1].priority + 1;
                } else {
                    this.menu.priority = 1;
                }
                this.menu.path = this.menu.priority + 1;
            },
            // 新增菜单数据保存到数据库
            handlerAddMenu() {
                axios.post("/menu/addMenu.do", this.menu).then((res) => {
                    this.dialogAddMenuVisible = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    this.loadMenu();
                    this.menu.parentMenuId = null;
                });
            },
            // 修改菜单
            editMenu(data) {
                this.dialogEditMenuVisible = true;
                this.menu = data;
            },
            // 修改菜单数据保存到数据库
            handlerEditMenu() {
                axios.post("/menu/editMenu.do", this.menu).then((res) => {
                    this.dialogEditMenuVisible = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    this.loadMenu();
                    this.menu.parentMenuId = null;
                });
            },
            // 删除菜单
            deleteMenu(data) {
                this.$confirm("此操作将永久删除 [" + data.name + "], 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    axios.get("/menu/deleteMenu.do?id=" + data.id).then((res) => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        });
                        this.loadMenu();
                        this.menu.parentMenuId = null;
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 加载菜单
            loadMenu() {
                axios.get("/menu/getMenus.do?id=0").then((res) => {
                    this.menuData = res.data.data.menus;
                    console.log(this.menuData);
                })
            }
        }
    })
</script>
</body>
</html>
